//
//  MXReportView.m
//  CatchBear
//
//  Created by 公司电脑 on 2019/1/11.
//  Copyright © 2019年 王启颖. All rights reserved.
//

#import "MXReportView.h"
#import "YZZBReportReasonCell.h"
#import "XMTextView.h"

@interface MXReportView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic,strong) NSArray *reportReasons;
@property (nonatomic,copy) NSString *selectedReason;
@property (nonatomic,strong) NSMutableArray *imgArr;
@property (weak, nonatomic) IBOutlet UIView *textView;
@property (strong, nonatomic) XMTextView *tv;
@property (copy, nonatomic) NSString *tv_str;
@end
@implementation MXReportView


- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self initCustomerControls];
}

- (void)initCustomerControls {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(kScreenWidth/3, 66);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.headerReferenceSize = CGSizeMake(0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView.collectionViewLayout = layout;
    [self.collectionView registerNib:[UINib nibWithNibName:@"YZZBReportReasonCell" bundle:nil] forCellWithReuseIdentifier:@"reportReasonCell"];
    
    [self.collectionView reloadData];
    
    self.tv = [[XMTextView alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, 166)];
    _tv.textFont = [UIFont systemFontOfSize:12];
    _tv.placeholder = @"请输入举报理由(选填),如果侵犯版权，请留下联系方式。";
    [self.textView addSubview:_tv];
    WEAKSELF
    _tv.textViewListening = ^(NSString *textViewStr) {
        weakSelf.tv_str = textViewStr;
        NSLog(@"tv监听输入的内容：%@",textViewStr);

    };
    
}

#pragma mark - 点击事件
- (IBAction)reportBtnClick:(id)sender {
    
    [self userComplaint];
}

#pragma mark - 网络请求 -
-(void)userComplaint{
    
    [[MXHttpManagerClass HttpRequstManager] PostURL:user_complaint params:@{@"user_complaint":@"1",@"complaint_text":self.tv_str} isHearderparam:YES success:^(NSInteger code, id result) {
        [SVProgressHUD dismiss];
        
        if (code == 0) {
            [SVProgressHUD showImage:NULL_IMAGE status:@"举报成功,工作人员会在24小时内处理。"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"参数错误"];
        }
        
    } fail:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

#pragma mark - UICollectionView dataSource/delegate methods

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {
    return self.reportReasons.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifierCell = @"reportReasonCell";
    
    YZZBReportReasonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierCell
                                                                           forIndexPath:indexPath];
    
    cell.reason = self.reportReasons[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.selectedReason = self.reportReasons[indexPath.row];
}

#pragma mark - UITextViewDelegate

/**
 *  当textView的文字改变就会调用
 */
//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//
//    if ([text isEqualToString:@"\n"])  //按会车可以改变
//    {
//        [textView resignFirstResponder];
//        return NO;
//    }
//
//    NSString * toBeString = [textView.text stringByReplacingCharactersInRange:range withString:text]; //得到输入框的内容
//
//    if ([toBeString length] > 0) {
//        [self.placehoderLabel setHidden:YES];
//    }else{
//        [self.placehoderLabel setHidden:NO];
//    }
//
//
//    if ([toBeString length] > 100) {
//        textView.text = [toBeString substringToIndex:100];
//        [SVProgressHUD showImage:NULL_IMAGE status:@"超过输入长度"];
//        //        self.residueLabel.text = [NSString stringWithFormat:@"%lu/50",(unsigned long)textView.text.length];
//        return NO;
//    }
//
//    //    self.residueLabel.text = [NSString stringWithFormat:@"%lu/50",(unsigned long)toBeString.length];
//
//    return YES;
//}
//
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    [self.textView endEditing:YES];
//}



#pragma mark - 懒加载 -
- (NSArray *)reportReasons{
    if (!_reportReasons) {
        _reportReasons = @[@"恶意骚扰",@"色情信息",@"垃圾广告",@"侵犯版权"];
    }
    return _reportReasons;
}

+ (instancetype)initMXReportView {
    return NSbunleloadNibName(@"MXReportView");
}

@end
